package com.pdstudio.apapter;

import java.util.List;

import com.pdstudio.app.Application;
import com.pdstudio.bean.Warning;
import com.pdstudio.view.ViewHolderWarning;
import com.pdstudio.weather.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class WarningAdapter extends BaseAdapter implements
		Application.EventHandler {

	private List<Warning> mWarnings;
	private LayoutInflater inflater;
	private Context mContext;
	private Application mApplication;

	public WarningAdapter(Context context, List<Warning> warnings) {
		mWarnings = warnings;
		inflater = LayoutInflater.from(context);
		mContext = context;
		Application.mListeners.add(this);
		mApplication = Application.getInstance();
	}

	@Override
	public int getCount() {
		if (mWarnings == null) {
			return 0;
		} else {
			return (mWarnings.size());
		}
	}

	@Override
	public Object getItem(int position) {
		if (mWarnings == null) {
			return null;
		} else {

			return mWarnings.get(position);

		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderWarning holder;
		try {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.gridview_item_warning,
						null);
				holder = new ViewHolderWarning();
				/** 得到各个控件的对象 */
				holder.warningImg = (ImageView) convertView
						.findViewById(R.id.warning_id);
				convertView.setTag(holder);// 绑定ViewHolder对象
			}

			else {
				holder = (ViewHolderWarning) convertView.getTag();// 取出ViewHolder对象
			}

			/** 设置TextView显示的内容，即我们存放在动态数组中的数据 */

			holder.warningImg.setBackgroundResource(mApplication
					.getWarningIcon(mWarnings.get(position).warningType));

		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub

	}

}
